package xyz.cloud.gulimall.product.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.cloud.gulimall.coupon.common.utils.PageUtils;
import xyz.cloud.gulimall.coupon.common.utils.Query;

import xyz.cloud.gulimall.product.dao.CategoryDao;
import xyz.cloud.gulimall.product.entity.CategoryEntity;
import xyz.cloud.gulimall.product.service.CategoryService;

/**
 * 商品三级分类
 */
@Service("categoryService")
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWhitTree() {
        //查询全部分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //组装 父子关系
        //使用stream 进行 过滤 获得 一级分类
        List<CategoryEntity> collect = categoryEntities.stream().filter(categoryEntity->
                        categoryEntity.getParentCid() == 0
                //组装 子菜单
        ).map((menu)->{
            menu.setChildren(getchildrens(menu,categoryEntities));
            return menu;
            //排序
        }).sorted((nemu1,nemu2)->{
            return (nemu1.getSort() == null?0 :nemu1.getSort() ) - (nemu2.getSort() == null ? 0: nemu2.getSort());
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void removeMenuByids(List<Long> asList) {
        //TODO 查询与此id是否有关联

        baseMapper.deleteBatchIds(asList);
    }


    //获取子菜单

    /**
     * private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
     *
     *         List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
     *             return categoryEntity.getParentCid() == root.getCatId();
     *         }).map(categoryEntity -> {
     *             //1、找到子菜单
     *             categoryEntity.setChildren(getChildrens(categoryEntity,all));
     *             return categoryEntity;
     *         }).sorted((menu1,menu2)->{
     *             //2、菜单的排序
     *             return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
     *         }).collect(Collectors.toList());
     *
     *         return children;
     *     }
     *
     * @param entity 参照
     * @param all 数据提供者
     * @return
     */
    private List<CategoryEntity> getchildrens(CategoryEntity entity,List<CategoryEntity> all){
        List<CategoryEntity> collect = all.stream().filter(categoryEntity -> {
                    return categoryEntity.getParentCid() == entity.getCatId();
        }).map(categoryEntity -> {
            /**
             * 此处 第一个参数 为 categoryEntity 并非 CategoryEntity entity 他 而是 由上一个 处理过的方法 返回的
             * 组装
             */
            categoryEntity.setChildren(getchildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((nemu1,nemu2)->{
            return (nemu1.getSort() == null?0 :nemu1.getSort() ) - (nemu2.getSort() == null ? 0: nemu2.getSort());
        }).collect(Collectors.toList());
        return collect;
    }
}